﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data;
using System.IO;
using Log4Net;

namespace PollServer
{
    public enum MsgQueueType
    {
        SWAP_QUEUE = 100,
        WAIT_SWAP_QUEUE = 101,
        WORK_MAIN_QUEUE_A = 102,
        WORK_MAIN_QUEUE_B = 103,
        WORK_MAIN_QUEUE_C = 104,
        WORK_MAIN_QUEUE_D = 105,
        WORK_MAIN_QUEUE_E = 106,
        WORK_MAIN_QUEUE_F = 107,
        WORK_MAIN_QUEUE_G = 108,
        WORK_MAIN_QUEUE_H = 109,
        WORK_MAIN_QUEUE_M = 110,
        QUEUE_SWAP_MAX = 3,
    };
    public class MsgThread 
    {   

        //线程类型
        public int _type;
        public GameDbDelegate _pushdelegate;
        //
#region 公有方法
        public MsgThread(MsgQueueType type)
        {
            _type = Convert.ToInt32(type);
        }

        public MsgThread(MsgQueueType type, GameDbDelegate pushdelegate)
        {
            _type = Convert.ToInt32(type);
            _pushdelegate = pushdelegate;
        }

        public static void run(object obj)
        {
            try
            {
                MsgThread mt = obj as MsgThread;
                int type = Convert.ToInt32(mt._type);
                if (Convert.ToInt32(MsgQueueType.SWAP_QUEUE) == type)
                {
                    mt.RunSwapWork();
                }
                else
                {
                    mt.RunExecWork();
                }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog("MsgThread 异常：", ex);
            }
        }
        //具体执行
        public void RunExecWork()
        {
            MsgQueue swap_queue = MsgQueue.GetMsgQueue(Convert.ToInt32(MsgQueueType.SWAP_QUEUE));
            MsgQueue _msg =  MsgQueue.GetMsgQueue(_type);
            while(true)
            {
                int _size = swap_queue.push_wait_queue(ref _msg, Convert.ToInt32(MsgQueueType.QUEUE_SWAP_MAX));
                if (_size > 0)
                {
                    run_all();
                }
                Thread.Sleep(50);
            }
	    }	

        //执行交换
        public void RunSwapWork()
        {
            MsgQueue wait_swap_queue = MsgQueue.GetMsgQueue(Convert.ToInt32(MsgQueueType.WAIT_SWAP_QUEUE));
            MsgQueue swap_queue = MsgQueue.GetMsgQueue(Convert.ToInt32(MsgQueueType.SWAP_QUEUE));

//             MsgQueue wait_swap_queue;
//             MsgQueue.GetMsgQueue(out wait_swap_queue, Convert.ToInt32(MsgQueueType.WAIT_SWAP_QUEUE));
//             MsgQueue swap_queue;
//             MsgQueue.GetMsgQueue(out swap_queue, Convert.ToInt32(MsgQueueType.SWAP_QUEUE));
            //
    	    while( true ){
	            //等待交换队列 至  交换队列
		        // int wait_swap_size = wait_swap_queue->size();
		        // while(wait_swap_size == 0){
	        	// }
		        //最大取1000个执行节点
                swap_queue.pop_pulse_queue(ref wait_swap_queue, Convert.ToInt32(MsgQueueType.QUEUE_SWAP_MAX));
                    //wait_swap_queue.push_queue(ref swap_queue, Convert.ToInt32(MsgQueueType.QUEUE_SWAP_MAX));
                Thread.Sleep(50);
	        }
        }

#endregion

#region 私有方法
        ///////私有不加锁方法///////
        private bool run_front()
        {
            MsgQueue _msg = MsgQueue.GetMsgQueue(_type);
            
            if (_msg == null) return false;
            
            if (_msg.size() == 0)
            {
                return false;
            }

            object _node = _msg.pop_front();
            if (_node != null)
            {
                //执行
                _pushdelegate(_node);
            }

            return true;
        }

        private void run_all()
        {
            while (run_front())
            {
                ;
            }
        }
#endregion
    }
}
